package com.example.androidcaculator.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.DialogFragment;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;

import com.example.androidcaculator.R;

import java.lang.reflect.Method;
import java.util.Calendar;

public class DateActivity extends AppCompatActivity {

    int year;
    int month;
    int day;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_date);
        Button startDateChoose = findViewById(R.id.startDateChoose);
        startDateChoose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showDatePickerDialog();
                startDateChoose.setText(year+"年"+month+"月"+day+"日");

            }
        });
        Button endDateChoose = findViewById(R.id.endDateChoose);
        endDateChoose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showDatePickerDialog();
                endDateChoose.setText(year+"年"+month+"月"+day+"日");
            }
        });

        Calendar calendar = Calendar.getInstance();
        year = calendar.get(Calendar.YEAR);
        month = calendar.get(Calendar.MONTH) + 1;
        day = calendar.get(Calendar.DAY_OF_MONTH);
        startDateChoose.setText(year+"年"+month+"月"+day+"日");
        endDateChoose.setText(year+"年"+month+"月"+day+"日");

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.date_menu, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        int id = item.getItemId();
        Intent intent;
        switch (id) {
            case R.id.dateCal_action_scientific:
                intent = new Intent(DateActivity.this, ScientificActivity.class);
                startActivity(intent);
                break;
            case R.id.dateCal_action_common:
                intent = new Intent(DateActivity.this, MainActivity.class);
                startActivity(intent);
                break;

            //转换器的选项
            case R.id.transform_length:
                intent = new Intent(DateActivity.this, ConverterLengthActivity.class);
                startActivity(intent);
                break;
            case R.id.transform_base:
                intent = new Intent(DateActivity.this, ConverterBaseActivity.class);
                startActivity(intent);
                break;
            case R.id.transform_currency:
                intent = new Intent(DateActivity.this, ConverterCurrencyActivity.class);
                startActivity(intent);
                break;

            case R.id.transform_volume:
                intent = new Intent(DateActivity.this, ConverterVolumeActivity.class);
                startActivity(intent);
                break;

            case R.id.dateCal_action_help:
                AlertDialog.Builder helpTxt = new AlertDialog.Builder(DateActivity.this);
                helpTxt.setMessage("这里是帮助文档\n本计算器由Dio开发").setTitle("Help");
                helpTxt.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {

                    }
                });
                helpTxt.show();

                break;
            case R.id.dateCal_action_exit:
                intent = new Intent(this, MainActivity.class);
                intent.putExtra(MainActivity.EXIST, true);
                startActivity(intent);

        }


        return super.onOptionsItemSelected(item);
    }

    @Override
    public boolean onMenuOpened(int featureId, Menu menu) {
        if (menu != null) {
            if (menu.getClass().getSimpleName().equalsIgnoreCase("MenuBuilder")) {
                try {
                    Method method = menu.getClass().getDeclaredMethod("setOptionalIconsVisible", Boolean.TYPE);
                    method.setAccessible(true);
                    method.invoke(menu, true);
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        }
        return super.onMenuOpened(featureId, menu);
    }

    public void showDatePickerDialog(){
        DialogFragment fragment = new DatePickerFragment();
        fragment.show(getSupportFragmentManager(),"datePicker");
    }

    public void updateDisplay(int year,int month,int day){
        this.year = year;
        this.month = month+1;
        this.day = day;

    }



}